package com.example.demo.generator.scaffold.controller;

import com.example.demo.common.BaseResponse;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.ResultUtils;
import com.example.demo.generator.scaffold.catalog.TemplateCategory;
import com.example.demo.generator.scaffold.service.TemplateCatalogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/generator/templates")
@RequiredArgsConstructor
@Tag(name = "模板目录", description = "查看脚手架模板清单")
public class TemplateCatalogController {

    private final TemplateCatalogService catalogService;

    @GetMapping
    @Operation(summary = "模板分类列表", description = "返回当前内置模板分类与简要信息")
    public BaseResponse<List<TemplateCategory>> listCategories() {
        return ResultUtils.success(catalogService.getCategories());
    }

    @GetMapping("/{id}")
    @Operation(summary = "模板分类详情", description = "根据分类 ID 返回模板清单")
    public BaseResponse<TemplateCategory> categoryDetail(@PathVariable String id) {
        return catalogService.getCategory(id)
                .map(ResultUtils::success)
                .orElseGet(() -> ResultUtils.error(ErrorCode.NOT_FOUND_ERROR, "模板分类不存在"));
    }
}


